package com.ajax;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ajax.formbean.UserBean;

public class AjaxServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AjaxServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doPost(request, response);

	}

	private static List<UserBean> getUserList() {
		List<UserBean> RD1 = new ArrayList<UserBean>();
		UserBean u1 = new UserBean("juans", 22, "music", 1);
		UserBean u2 = new UserBean("lily", 22, "shopping", 1);
		UserBean u3 = new UserBean("polly", 22, "reading", 1);
		UserBean u4 = new UserBean("MS li", 22, "basketball", 2);
		UserBean u5 = new UserBean("wu lis", 22, "football", 2);

		RD1.add(u1);
		RD1.add(u2);
		RD1.add(u3);
		RD1.add(u4);
		RD1.add(u5);

		return RD1;

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		List<UserBean> list = getUserList();
		request.setAttribute("users", list);
		request.getRequestDispatcher("../forwad.jsp").forward(request, response);

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	@Override
	public void init() throws ServletException {
		// Put your code here
	}

}
